import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img_rgb = cv.imread('../pic/mario.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('../pic/mario_coin.png', 0)
# cv.imshow("img", img_gray)
# cv.imshow("template", template)

w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray, template, cv.TM_CCOEFF_NORMED)
threshold = 0.8
# 在res中遍历所有的大于阈值点，结果是点的坐标
loc = np.where(res >= threshold)
# zip函数将列表打包成元组
for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)

cv.imshow('res.png', img_rgb)

cv.waitKey()
cv.destroyAllWindows()
